export default {
	baseUrl: 'http://47.105.171.139:8006/', //线上
	// baseUrl: 'http://47.105.171.139:8006/', //开发
	header: {},
	firstTime: 0,
	dance(fn, wait) { //防抖
		let timer = null
		return function() {
			if (timer) {
				console.log('点击太频繁了')
				clearTimeout(timer)
			}
			timer = setTimeout(() => {
				fn()
				clearTimeout(timer)
			}, wait)
		}
	},
	debounce(func, wait) {
		let timer;
		return function() {
			if (timer) {
				clearTimeout(timer);
			}
			timer = setTimeout(() => {
				func()
				clearTimeout(timer)
			}, wait)
		}
	},
	throttle(wait) { //节流 利用闭包使得lasttime长留内存中，不建议
		let lasttime = 0;
		return function(e) {
			let nowtime = new Date().getTime();
			if (nowtime - lasttime > wait) { //
				lasttime = nowtime;
				return false
			} else {
				return true
			}
		}
	},
	throttle1(wait) { //节流 单位时间内重复操作无效
		let nowtime = new Date().getTime();
		if (nowtime - this.firstTime > wait) { //可以操作
			this.firstTime = nowtime;
			return false
		} else { //不能操作
			return true
		}
	},
	get(requestUrl, data, callback, failcallback, loadTitle = '加载中...', ) { //callback 成功回调   failcallback失败回调
		uni.showLoading({
			title: loadTitle
		});
		let token = uni.getStorageSync('token_key');
		if (token) {
			this.header.Authorization = ' Bearer ' + token
		}
		uni.request({
			url: (this.baseUrl + requestUrl).trim(),
			data: data,
			method: 'GET',
			header: this.header,
			success: (res) => {
				uni.hideLoading();
				if (res.data.code == 0) {
					callback && callback(res.data)
				} else {
					uni.showToast({
						title: res.data.data,
						icon: 'none'
					})
				}
			},
			fail: (err) => {
				uni.hideLoading();
				console.log(err)
				uni.showToast({
					title: err.errMsg,
					duration: 2000,
					icon: 'none'
				})
				failcallback && failcallback(err)
			},
			complete: () => {

			}
		})
	},
	post(requestUrl, data, callback, failcallback, loadTitle = '加载中...', throttle =
		false) { //callback 成功回调   failcallback失败回调
		if (throttle) { //是否启用节流
			if (this.throttle1(1000)) { //1秒内重复点击不能触发请求
				console.log('你频繁点击了，所以限制了请求')
				return
			}
		}
		uni.showLoading({
			title: loadTitle
		});
		let token = uni.getStorageSync('token_key');
		if (token) {
			this.header.Authorization = ' Bearer ' + token
		}
		uni.request({
			url: (this.baseUrl + requestUrl).trim(),
			data: data,
			method: 'POST',
			header: this.header,
			success: (res) => {
				//延长遮罩时间，不让用户频繁点击操作
				// setTimeout(() => {
				uni.hideLoading();
				// }, 800)
				callback && callback(res.data)
				return
				if (res.data.code == 0) {
					callback && callback(res.data)
				} else {
					console.log(res)
					uni.showToast({
						title: res.data.data,
						icon: 'none'
					})
				}
			},
			fail: (err) => {
				uni.hideLoading();
				console.log(err)
				uni.showToast({
					title: err.errMsg,
					icon: 'none',
					duration: 2000
				})
				failcallback && failcallback(err)
			},
			complete: () => {

			}
		})
	}
}
